﻿Imports System.Data
Imports MyGeneration.dOOdads

Public Class rc_Clientes

    Private _MessageError As Exception = Nothing
    Public Property MessageError() As Exception
        Get
            Return _MessageError
        End Get
        Set(value As Exception)
            _MessageError = value
        End Set
    End Property


#Region "Clientes"

    Public Function Insert_Clientes(conn As String, nombre As String, direccion As String, telefono As String, celular As String,
                                    codCiudad As Integer, ruc As String, saldo As Decimal, codTipoCliente As Integer,
                                    codUsuario As Integer) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim cliente As New Class_RC_Data.Class_RC.CLIENTES(conn)

            cliente.AddNew()
            cliente.Nombre = nombre
            cliente.Direccion = direccion
            cliente.Telefono = telefono
            cliente.Celular = celular
            cliente.CodCiudad = codCiudad
            cliente.Ruc = ruc
            cliente.Saldo = saldo
            cliente.CodTipoCliente = codTipoCliente
            cliente.CodUsuario = codUsuario
            cliente.FechaActualizacion = DateTime.Now
            cliente.Save()

            tx.CommitTransaction()

            Return cliente.CodCliente

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try

    End Function

    Public Function Update_Clientes(conn As String, codCliente As Integer, nombre As String, direccion As String, telefono As String, celular As String,
                                    codCiudad As Integer, ruc As String, saldo As Decimal, codTipoCliente As Integer,
                                    codUsuario As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim cliente As New Class_RC_Data.Class_RC.CLIENTES(conn)
            cliente.Where.CodCliente.Value = codCliente

            If cliente.Query.Load Then

                cliente.Nombre = nombre
                cliente.Direccion = direccion
                cliente.Telefono = telefono
                cliente.Celular = celular
                cliente.CodCiudad = codCiudad
                cliente.Ruc = ruc
                cliente.Saldo = saldo
                cliente.CodTipoCliente = codTipoCliente
                cliente.CodUsuario = codUsuario
                cliente.FechaActualizacion = DateTime.Now
                cliente.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_Clientes(conn As String, codCliente As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim cliente As New Class_RC_Data.Class_RC.CLIENTES(conn)
            cliente.Where.CodCliente.Value = codCliente

            If cliente.Query.Load Then

                cliente.MarkAsDeleted()
                cliente.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function get_Clientes_All(conn As String, codCliente As Integer, nombre As String, ruc As String,
                                     codTipoCliente As Integer, codCiudad As Integer) As DataTable
        Try

            Dim vClientes As New Class_RC_Data.Class_RC.VistaClientes(conn)

            If codCliente > 0 Then
                vClientes.Where.CodCliente.Value = codCliente
            End If

            If nombre <> "" Then
                vClientes.Where.Nombre.Value = "%" & nombre & "%"
                vClientes.Where.Nombre.Operator = WhereParameter.Operand.Like
            End If

            If codTipoCliente > 0 Then
                vClientes.Where.CodTipoCliente.Value = codTipoCliente
            End If

            If codCiudad > 0 Then
                vClientes.Where.CodCiudad.Value = codCiudad
            End If

            vClientes.Query.AddOrderBy("nombre", WhereParameter.Dir.ASC)

            If vClientes.Query.Load Then
                If vClientes.DefaultView.Table.Rows.Count > 0 Then
                    Return vClientes.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_vendedores(conn As String) As DataTable
        Try

            Dim vUsu As New Class_RC_Data.Class_RC.USUARIOS(conn)

            If vUsu.Query.Load Then
                If vUsu.DefaultView.Table.Rows.Count > 0 Then
                    Return vUsu.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_tipo_Clientes(conn As String) As DataTable
        Try

            Dim vTipoClientes As New Class_RC_Data.Class_RC.TIPOCLIENTES(conn)

            If vTipoClientes.Query.Load Then
                If vTipoClientes.DefaultView.Table.Rows.Count > 0 Then
                    Return vTipoClientes.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_Ciudades(conn As String) As DataTable
        Try

            Dim vTipoCiudad As New Class_RC_Data.Class_RC.CIUDAD(conn)

            If vTipoCiudad.Query.Load Then
                If vTipoCiudad.DefaultView.Table.Rows.Count > 0 Then
                    Return vTipoCiudad.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function Insert_Contactos(conn As String, nombre As String, telefono As String, celular As String, codCliente As Integer, cargo As String, email As String) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim cliente As New Class_RC_Data.Class_RC.CONTACTOS(conn)

            cliente.AddNew()
            cliente.Nombre = nombre
            cliente.Telefono = telefono
            cliente.Celular = celular
            cliente.CodCliente = codCliente
            cliente.Cargo = cargo
            cliente.Email = email
            cliente.Save()

            tx.CommitTransaction()

            Return cliente.CodCliente

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try

    End Function

    Public Function Delete_Contactos(conn As String, codContacto As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim cliente As New Class_RC_Data.Class_RC.CONTACTOS(conn)
            cliente.Where.CodContacto.Value = codContacto

            If cliente.Query.Load Then

                cliente.MarkAsDeleted()
                cliente.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function get_Clientes_Contactos_All(conn As String, codCliente As Integer) As DataTable
        Try

            Dim vClientes As New Class_RC_Data.Class_RC.CONTACTOS(conn)

            If codCliente > 0 Then
                vClientes.Where.CodCliente.Value = codCliente
            End If

            If vClientes.Query.Load Then
                If vClientes.DefaultView.Table.Rows.Count > 0 Then
                    Return vClientes.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

#End Region

#Region "TipoClientes"


    Public Function Insert_TipoCliente(ByVal conn As String, ByVal descripcion As String, ByVal limitecredito As Decimal, ByVal porcdescuento As Integer) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim tipocliente As New Class_RC_Data.Class_RC.TIPOCLIENTES(conn)

            tipocliente.AddNew()
            tipocliente.Descripcion = descripcion
            tipocliente.Limitecredito = limitecredito
            tipocliente.Porcdescuento = porcdescuento
            tipocliente.Save()

            tx.CommitTransaction()

            Return tipocliente.Codtipocliente

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try

    End Function


    Public Function Update_TipoCliente(ByVal conn As String, codtipocliente As Integer , ByVal descripcion As String, ByVal limitecredito As Decimal, ByVal porcdescuento As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim tipocliente As New Class_RC_Data.Class_RC.TIPOCLIENTES(conn)
            tipocliente.Where.Codtipocliente.Value = codtipocliente

            If tipocliente.Query.Load Then

                tipocliente.Descripcion = descripcion
                tipocliente.Limitecredito = limitecredito
                tipocliente.Porcdescuento = porcdescuento

                tipocliente.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_TipoClientes(ByVal conn As String, ByVal codtipocliente As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim tipocliente As New Class_RC_Data.Class_RC.TIPOCLIENTES(conn)
            tipocliente.Where.Codtipocliente.Value = codtipocliente

            If tipocliente.Query.Load Then

                tipocliente.MarkAsDeleted()
                tipocliente.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function
    Public Function get_TipoClientes_All(ByVal conn As String, ByVal codtipocliente As Integer, ByVal descripcion As String) As DataTable
        Try

            Dim vtipocliente As New Class_RC_Data.Class_RC.TIPOCLIENTES(conn)

            If codtipocliente > 0 Then
                vtipocliente.Where.Codtipocliente.Value = codtipocliente
            End If

            If descripcion <> "" Then
                vtipocliente.Where.Descripcion.Value = "%" & descripcion & "%"
                vtipocliente.Where.Descripcion.Operator = WhereParameter.Operand.Like
            End If


            If vtipocliente.Query.Load Then
                If vtipocliente.DefaultView.Table.Rows.Count > 0 Then
                    Return vtipocliente.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function



#End Region


End Class
